<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;
use app\admin\model\RouteModel;
use app\portal\service\CasesService;
use cmf\controller\AdminBaseController;
use app\portal\model\PortalCasesModel;
use think\Db;
use app\admin\model\ThemeModel;

ini_set("display_errors","On");
error_reporting(E_ALL);

class AdminCasesController extends AdminBaseController
{
   /**
    * 设计案例列表
    * @adminMenu(
    *     'name'   => '设计案例管理',
    *     'parent' => 'portal/AdminIndex/default',
    *     'display'=> true,
    *     'hasView'=> true,
    *     'order'  => 10000,
    *     'icon'   => '',
    *     'remark' => '设计案例列表',
    *     'param'  => ''
    * )
    */
   public function index()
   {
      $param = $this->request->param();

      $casesService = new CasesService();
      $data        = $casesService->adminCasesList($param);

      $data->appends($param);
      $this->assign('start_time', isset($param['start_time']) ? $param['start_time'] : '');
      $this->assign('end_time', isset($param['end_time']) ? $param['end_time'] : '');
      $this->assign('cases', $data->items());
      $this->assign('page', $data->render());
      return $this->fetch();
   }

   /**
    * 添加案例
    * @adminMenu(
    *     'name'   => '添加案例',
    *     'parent' => 'index',
    *     'display'=> false,
    *     'hasView'=> true,
    *     'order'  => 10000,
    *     'icon'   => '',
    *     'remark' => '添加案例',
    *     'param'  => ''
    * )
    */
   public function add()
   {
      $themeModel        = new ThemeModel();
      $casesThemeFiles = $themeModel->getActionThemeFiles('portal/Cases/index');
      $this->assign('cases_theme_files', $casesThemeFiles);
      return $this->fetch();
   }

   /**
    * 添加案例提交
    * @adminMenu(
    *     'name'   => '添加案例提交',
    *     'parent' => 'index',
    *     'display'=> false,
    *     'hasView'=> false,
    *     'order'  => 10000,
    *     'icon'   => '',
    *     'remark' => '添加案例提交',
    *     'param'  => ''
    * )
    */
   public function addPost()
   {
      if ($this->request->isPost()) {
         $data   = $this->request->param();
         $post   = $data['post'];
//         $result = $this->validate($post, 'AdminArticle');
//         if ($result !== true) {
//            $this->error($result);
//         }

         $portalCasesModel = new portalCasesModel();

         if (!empty($data['photo_names']) && !empty($data['photo_urls'])) {
            $data['post']['more']['photos'] = [];
            foreach ($data['photo_urls'] as $key => $url) {
               $photoUrl = cmf_asset_relative_url($url);
               array_push($data['post']['more']['photos'], ["url" => $photoUrl, "name" => $data['photo_names'][$key]]);
            }
         }
         if (!empty($data['file_names']) && !empty($data['file_urls'])) {
            $data['post']['more']['files'] = [];
            foreach ($data['file_urls'] as $key => $url) {
               $fileUrl = cmf_asset_relative_url($url);
               array_push($data['post']['more']['files'], ["url" => $fileUrl, "name" => $data['file_names'][$key]]);
            }
         }

         $portalCasesModel->adminAddCases($data['post']);

         $data['post']['id'] = $portalCasesModel->id;


         $this->success('添加成功!',url('AdminCases/edit', ['id' => $portalCasesModel->id]));
      }

   }

   /**
    * 编辑案例
    * @adminMenu(
    *     'name'   => '编辑案例',
    *     'parent' => 'index',
    *     'display'=> false,
    *     'hasView'=> true,
    *     'order'  => 10000,
    *     'icon'   => '',
    *     'remark' => '编辑案例',
    *     'param'  => ''
    * )
    */
   public function edit()
   {
      $id = $this->request->param('id', 0, 'intval');
      $portalCasesModel = new portalCasesModel();
      $post            = $portalCasesModel->where('id', $id)->find();

      $themeModel        = new ThemeModel();
      $casesThemeFiles = $themeModel->getActionThemeFiles('portal/Cases/index');
      $this->assign('cases_theme_files', $casesThemeFiles);
      $this->assign('post', $post);

      return $this->fetch();
   }

   /**
    * 编辑文案提交
    * @adminMenu(
    *     'name'   => '编辑文案提交',
    *     'parent' => 'index',
    *     'display'=> false,
    *     'hasView'=> false,
    *     'order'  => 10000,
    *     'icon'   => '',
    *     'remark' => '编辑文案提交',
    *     'param'  => ''
    * )
    */
   public function editPost()
   {
      if ($this->request->isPost()) {
         $data   = $this->request->param();
         $post   = $data['post'];

         $portalCasesModel = new PortalCasesModel();

         if (!empty($data['photo_names']) && !empty($data['photo_urls'])) {
            $data['post']['more']['photos'] = [];
            foreach ($data['photo_urls'] as $key => $url) {
               $photoUrl = cmf_asset_relative_url($url);
               array_push($data['post']['more']['photos'], ["url" => $photoUrl, "name" => $data['photo_names'][$key]]);
            }
         }

         if (!empty($data['file_names']) && !empty($data['file_urls'])) {
            $data['post']['more']['files'] = [];
            foreach ($data['file_urls'] as $key => $url) {
               $fileUrl = cmf_asset_relative_url($url);
               array_push($data['post']['more']['files'], ["url" => $fileUrl, "name" => $data['file_names'][$key]]);
            }
         }

         $portalCasesModel->adminEditCases($data['post']);


         $this->success('保存成功!');

      }
   }

   /**
    * 案例删除
    * @adminMenu(
    *     'name'   => '案例删除',
    *     'parent' => 'index',
    *     'display'=> false,
    *     'hasView'=> false,
    *     'order'  => 10000,
    *     'icon'   => '',
    *     'remark' => '案例删除',
    *     'param'  => ''
    * )
    */
   public function delete()
   {
      $param           = $this->request->param();
      $portalCasesModel = new PortalCasesModel();

      if (isset($param['id'])) {
         $id           = $this->request->param('id', 0, 'intval');
         $result       = $portalCasesModel->where(['id' => $id])->find();
         $data         = [
             'object_id'   => $result['id'],
             'create_time' => time(),
             'table_name'  => 'portal_cases',
             'name'        => $result['post_title'],
             'user_id'=>cmf_get_current_admin_id()
         ];
         $resultPortal = $portalCasesModel
             ->where(['id' => $id])
             ->update(['delete_time' => time()]);
         if ($resultPortal) {

            Db::name('recycleBin')->insert($data);
         }
         $this->success("删除成功！", '');

      }

      if (isset($param['ids'])) {
         $ids     = $this->request->param('ids/a');
         $recycle = $portalCasesModel->where(['id' => ['in', $ids]])->select();
         $result  = $portalCasesModel->where(['id' => ['in', $ids]])->update(['delete_time' => time()]);
         if ($result) {
            foreach ($recycle as $value) {
               $data = [
                   'object_id'   => $value['id'],
                   'create_time' => time(),
                   'table_name'  => 'portal_cases',
                   'name'        => $value['post_title'],
                   'user_id'=>cmf_get_current_admin_id()
               ];
               Db::name('recycleBin')->insert($data);
            }
            $this->success("删除成功！", '');
         }
      }
   }

   /**
    * 案例发布
    * @adminMenu(
    *     'name'   => '案例发布',
    *     'parent' => 'index',
    *     'display'=> false,
    *     'hasView'=> false,
    *     'order'  => 10000,
    *     'icon'   => '',
    *     'remark' => '案例发布',
    *     'param'  => ''
    * )
    */
   public function publish()
   {
      $param           = $this->request->param();
      $portalCasesModel = new PortalCasesModel();

      if (isset($param['ids']) && isset($param["yes"])) {
         $ids = $this->request->param('ids/a');

         $portalCasesModel->where(['id' => ['in', $ids]])->update(['post_status' => 1, 'published_time' => time()]);

         $this->success("发布成功！", '');
      }

      if (isset($param['ids']) && isset($param["no"])) {
         $ids = $this->request->param('ids/a');

         $portalCasesModel->where(['id' => ['in', $ids]])->update(['post_status' => 0]);

         $this->success("取消发布成功！", '');
      }

   }

   /**
    * 案例置顶
    * @adminMenu(
    *     'name'   => '案例置顶',
    *     'parent' => 'index',
    *     'display'=> false,
    *     'hasView'=> false,
    *     'order'  => 10000,
    *     'icon'   => '',
    *     'remark' => '案例置顶',
    *     'param'  => ''
    * )
    */
   public function top()
   {
      $param           = $this->request->param();
      $portalCasesModel = new PortalCasesModel();

      if (isset($param['ids']) && isset($param["yes"])) {
         $ids = $this->request->param('ids/a');

         $portalCasesModel->where(['id' => ['in', $ids]])->update(['is_top' => 1]);

         $this->success("置顶成功！", '');

      }

      if (isset($_POST['ids']) && isset($param["no"])) {
         $ids = $this->request->param('ids/a');

         $portalCasesModel->where(['id' => ['in', $ids]])->update(['is_top' => 0]);

         $this->success("取消置顶成功！", '');
      }
   }

   /**
    * 案例推荐
    * @adminMenu(
    *     'name'   => '案例推荐',
    *     'parent' => 'index',
    *     'display'=> false,
    *     'hasView'=> false,
    *     'order'  => 10000,
    *     'icon'   => '',
    *     'remark' => '案例推荐',
    *     'param'  => ''
    * )
    */
   public function recommend()
   {
      $param           = $this->request->param();
      $portalCasesModel = new PortalCasesModel();

      if (isset($param['ids']) && isset($param["yes"])) {
         $ids = $this->request->param('ids/a');

         $portalCasesModel->where(['id' => ['in', $ids]])->update(['recommended' => 1]);

         $this->success("推荐成功！", '');

      }
      if (isset($param['ids']) && isset($param["no"])) {
         $ids = $this->request->param('ids/a');

         $portalCasesModel->where(['id' => ['in', $ids]])->update(['recommended' => 0]);

         $this->success("取消推荐成功！", '');

      }
   }

   /**
    * 文章排序
    * @adminMenu(
    *     'name'   => '文章排序',
    *     'parent' => 'index',
    *     'display'=> false,
    *     'hasView'=> false,
    *     'order'  => 10000,
    *     'icon'   => '',
    *     'remark' => '文章排序',
    *     'param'  => ''
    * )
    */
   public function listOrder()
   {
      parent::listOrders(Db::name('portal_category_post'));
      $this->success("排序更新成功！", '');
   }

   public function move()
   {

   }

   public function copy()
   {

   }




}
